package cn.wolfcode.web.controller;

import cn.wolfcode.enums.BusinessReportEnum;
import cn.wolfcode.enums.ConsumptionStatusEnum;
import cn.wolfcode.qo.BusinessReportQueryObject;
import cn.wolfcode.service.IBusinessReportService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.util.RequiredPermission;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RequestMapping("/businessReport")
@Controller
public class BusinessReportController {
    @Autowired
    private IBusinessReportService businessReportService;
    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") BusinessReportQueryObject queryObject) {
        //过滤查询的数据
        PageInfo<HashMap> pageInfo = businessReportService.selectReport(queryObject);
        model.addAttribute("pageInfo", pageInfo);
        //按门店查询的下拉框数据
        model.addAttribute("businesses", businessService.selectAll());
        //结算单状态下拉框数据
        model.addAttribute("consumptionStatus", ConsumptionStatusEnum.values());
        //门店报表年月日选择下拉框数据
        model.addAttribute("businessReport", BusinessReportEnum.values());
        return "/businessReport/list";
    }

    /*1.准备五个集合容器
     * 2.遍历集合
     * 3.共享到页面*/
    @RequestMapping("/listBar")
    @RequiredPermission(name = "门店收入柱状图报表", expression = "businessReport:listBar")
    public String listBar(Model model, @ModelAttribute("qo") BusinessReportQueryObject queryObject) {


        //1.准备五个容器，放查询出来的数据：总金额，总订单数，总收益
        List<Object> groupTypeList = new ArrayList<>();//分组类型
        List<Object> totalDiscountList = new ArrayList<>();//总优惠金额
        List<Object> totalConsumptionAmountList = new ArrayList<>();//总消费金额
        List<Object> payAmountList = new ArrayList<>();//实收金额
        List<Object> totalOrderItemsList = new ArrayList<>();//总订单数

        //报表的数据不需要分页，所以设置为0
        queryObject.setPageSize(0);
        List<HashMap> list = businessReportService.selectReport(queryObject).getList();

        //2.遍历得到的info集合(每一条数据是一个map集合)
        list.forEach(hashMap -> {
            groupTypeList.add(hashMap.get("groupType"));//分组类型
            totalConsumptionAmountList.add(hashMap.get("totalAmount"));//总消费
            totalDiscountList.add(hashMap.get("discountAmount"));//总优惠
            payAmountList.add(hashMap.get("payAmount"));//实收金额
            totalOrderItemsList.add(hashMap.get("number"));//总订单条数
        });

        //3.共享到页面:将数组转化为Json字符串(格式为[“”，“”，“”])

        model.addAttribute("groupTypeList",JSON.toJSONString(groupTypeList));
        model.addAttribute("totalConsumptionAmountList",JSON.toJSONString(totalConsumptionAmountList));
        model.addAttribute("totalDiscountList",JSON.toJSONString(totalDiscountList));
        model.addAttribute("payAmountList",JSON.toJSONString(payAmountList));
        model.addAttribute("totalOrderItemsList",JSON.toJSONString(totalOrderItemsList));
        return "/businessReport/listBar";
    }
}
